package leetcode.code0664;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

/**
 * 执行用时：42 ms, 在所有 Java 提交中击败了5.54%的用户
 *
 * 内存消耗：38.5 MB, 在所有 Java 提交中击败了36.53%的用户
 *
 * 通过测试用例：200 / 200
 */
public class Solution3 implements IDebug {

	public int strangePrinter(String s) {
		int len = s.length();
		Integer[][] dp = new Integer[len][len];
		return this.f(s.toCharArray(), 0, len - 1, dp);
	}

	private int f(char[] cs, int l, int r, Integer[][] dp) {
		if (l == r) {
			return 1;
		}
		if (dp[l][r] != null) {
			return dp[l][r];
		}
		int ans = Integer.MAX_VALUE;
		for (int k = l; k < r; k++) {
			int ansk = this.f(cs, l, k, dp) + this.f(cs, k + 1, r, dp);
			if (cs[l] == cs[k + 1]) {
				ansk -= 1;
			}
			ans = Math.min(ansk, ans);
		}
		dp[l][r] = ans;
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(2, this.strangePrinter("aaabbb"));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(6, this.strangePrinter("sdfasdf"));

	}

	@Override
	public void debug3() {
		HelpDebug.compare(2, this.strangePrinter("aba"));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution3 so = new Solution3();
		so.debug1();
		so.debug2();
		so.debug3();
//		D.debug14(so);

	}

}
